<template>
    <div class="min-h-screen flex items-center justify-center bg-gray-50 py-12 px-4 sm:px-6 lg:px-8">
        <div class="max-w-md w-full space-y-8">
            <div>
                <h2 class="mt-6 text-center text-3xl font-extrabold text-gray-900">登录到你的账户</h2>
            </div>
            <div class="mt-8 space-y-6">
                <div class="rounded-md shadow-sm -space-y-px">
                    <div class="mb-4">
                        <label for="username" class="sr-only">用户名</label>
                        <input v-model="username" type="text" autocomplete="username" required
                            class="appearance-none rounded-none relative block w-full px-3 py-2 border border-gray-300 placeholder-gray-500 text-gray-900 rounded-t-md focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 focus:z-10 sm:text-sm"
                            placeholder="用户名" @keyup.enter="handleLogin">
                    </div>
                    <div>
                        <label for="password" class="sr-only">密码</label>
                        <input v-model="password" type="password" autocomplete="current-password" required
                            class="appearance-none rounded-none relative block w-full px-3 py-2 border border-gray-300 placeholder-gray-500 text-gray-900 rounded-b-md focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 focus:z-10 sm:text-sm"
                            placeholder="密码" @keyup.enter="handleLogin">
                    </div>
                </div>

                <div class="flex items-center justify-between">
                    <div class="flex items-center">
                        <input id="remember-me" name="remember-me" type="checkbox"
                            class="h-4 w-4 text-indigo-600 focus:ring-indigo-500 border-gray-300 rounded">
                        <label for="remember-me" class="ml-2 block text-sm text-gray-900">记住我</label>
                    </div>

                    <div class="text-sm">
                        <a href="#" class="font-medium text-indigo-600 hover:text-indigo-500">忘记密码？</a>
                    </div>
                </div>

                <div>
                    <button  @click="handleLogin"
                        class="group relative w-full flex justify-center py-2 px-4 border border-transparent text-sm font-medium rounded-md text-white bg-indigo-600 hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500">
                        <span class="absolute left-0 inset-y-0 flex items-center pl-3">
                            <i class="fa fa-lock text-indigo-200 group-hover:text-indigo-100"></i>
                        </span>
                        登录
                    </button>
                </div>
            </div>
        </div>
    </div>
</template>

<script setup>
import CryptoJS from 'crypto-js'

const username = ref('')
const password = ref('')
const router = useRouter()

const { Message } = useMessageBox()
const { login } = useAuth()

const handleLogin = async() => {
    const res = await login({
        username: username.value,
        password: CryptoJS.SHA256(password.value).toString(CryptoJS.enc.Hex)
    })
    if (res.code != 200) {
        Message.error('用户名或密码错误', { width: 'auto' })
    } else {
        router.push('/') 
    }
    
}
</script>